﻿using DNFImagePacks2Manager.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DNFImagePacks2Manager.Helpers
{
    public sealed class CustomHelper
    {
        // 个性化配置文件夹名
        private readonly static string csmFolderName = "custom";
        public readonly static string LineSuffix = ".csm";
        public readonly static string JsonSuffix = ".csm2";

        // 获取个性化配置路径
        public static string GetCustomFolder()
        {
            string csmFolder = Path.Combine(FileHelper.GetAppFolder, csmFolderName);
            if (!Directory.Exists(csmFolder))
            {
                Directory.CreateDirectory(csmFolder);
            }
            return csmFolder;
        }

        // 获取个性化配置列表
        public static List<NPKCustomBase> GetCustomList()
        {
            string customFolder = GetCustomFolder();
            List<NPKCustomBase> customList = [];
            foreach (string path in Directory.GetFiles(customFolder))
            {
                string filename = Path.GetFileName(path);
                if (string.Equals(Path.GetExtension(filename), JsonSuffix, StringComparison.OrdinalIgnoreCase))
                {
                    customList.Add(new NPKCustomJson(filename, customFolder));
                    continue;
                }
                if (string.Equals(Path.GetExtension(filename), LineSuffix, StringComparison.OrdinalIgnoreCase))
                {
                    customList.Add(new NPKCustomLine(filename, customFolder));
                    continue;
                }
            }
            return customList;
        }

        public static bool ExistCustomFile(string fileName)
        {
            return ExistCustomFile(fileName, JsonSuffix);
        }

        public static bool ExistCustomFile(string fileName, string suffix)
        {
            string csmFile = Path.Combine(GetCustomFolder(), fileName + suffix);
            return File.Exists(csmFile);
        }

        // 保存个性化配置文件
        public static bool SaveCustomFile(string customName, List<NPKItemBase> npkList)
        {
            try
            {
                string fileName = customName + JsonSuffix;
                string customFile = Path.Combine(GetCustomFolder(), fileName);

                List<NPKCustomJsonNode> customList = [];

                string ImagePrefix = NPKItemImage.NamePrefix;
                string SoundPrefix = NPKItemSound.NamePrefix;

                foreach (var npkItem in npkList)
                {
                    if (npkItem is NPKItemImage)
                    {
                        customList.Add(new NPKCustomJsonNode(npkItem.Name, ImagePrefix));
                        continue;
                    }
                    if (npkItem is NPKItemSound)
                    {
                        customList.Add(new NPKCustomJsonNode(npkItem.Name, SoundPrefix));
                        continue;
                    }
                }

                var settings = new JsonSerializerSettings
                {
                    Formatting = Formatting.Indented,
                    ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
                };

                string json = JsonConvert.SerializeObject(customList, settings);

                File.WriteAllText(customFile, json, Encoding.UTF8);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

    }
}
